﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DentalSoftware
{
    public partial class ThemBenhNhan : Form
    {
        private int status;

        private BenhNhanDTO benhnhan;

        public ThemBenhNhan()
        {
            InitializeComponent();
            status = 0;
            txtMaHoSo.Text = BenhNhanBUS.GetMaxMa().ToString();
        }

        public ThemBenhNhan(BenhNhanDTO bn)
        {
            InitializeComponent();
            benhnhan = bn;            
            status = 1;        
        }

        private void ThemBenhNhan_Load(object sender, EventArgs e)
        {           
            if (status == 1)
            {
                LoadBenhNhan();
            }
            else
            {
                txtMaHoSo.Text = BenhNhanBUS.GetMaxMa().ToString();
            }
        }

        private void LoadBenhNhan()
        {
            if (benhnhan != null)
            {
                txtMaHoSo.Text = benhnhan.Ma.ToString();
                txtHoTen.Text = benhnhan.Hoten;
                txtDienThoai.Text = benhnhan.Dienthoai;
                txtDiaChi.Text = benhnhan.Diachi;
                txtTuoi.Text = (DateTime.Now.Year - int.Parse(benhnhan.Ngaysinh.Year.ToString())).ToString();
                if (benhnhan.DaChupPhim == 1)
                    ckbDaChupPhim.Checked = true;
                else
                    ckbDaChupPhim.Checked = false;
                if (benhnhan.Gioitinh == 0)
                {
                    rbnNam.Checked = true;
                    rbnNu.Checked = false;
                }
                else
                {
                    rbnNam.Checked = false;
                    rbnNu.Checked = true;
                }
            }
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (txtBenhKhac.Enabled == false)
                txtBenhKhac.Enabled = true;
            else
                txtBenhKhac.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            BenhNhanDTO benhnhan = new BenhNhanDTO();
            benhnhan.Ma = int.Parse(txtMaHoSo.Text);
            benhnhan.Hoten = txtHoTen.Text;
            if (rbnNam.Checked == true)
                benhnhan.Gioitinh = 0;
            else
                benhnhan.Gioitinh = 1;
            benhnhan.Hinhanh = "Chưa có";
            benhnhan.Khamdinhki = 1;
            benhnhan.Maloai = 1;
            benhnhan.Manghenghiep = 1;
            benhnhan.Maquan = 1;
            benhnhan.Diachi = txtDiaChi.Text;
            benhnhan.Dienthoai = txtDienThoai.Text;
            if (ckbDaChupPhim.Checked)
                benhnhan.DaChupPhim = 1;
            else
                benhnhan.DaChupPhim = 0;
            if (txtTuoi.Text == "")
                txtTuoi.Text = "0";
            else if (Int32.Parse(txtTuoi.Text) > 1000)
                txtTuoi.Text = (DateTime.Now.Year - Int32.Parse(txtTuoi.Text)).ToString();
            benhnhan.Ngaysinh = new DateTime(DateTime.Now.Year - int.Parse(txtTuoi.Text), 1, 1);
            benhnhan.DuaTruoc = 0;
            benhnhan.ChiPhi = 0;
            bool kq = true;
            switch (status)
            {
                case 0:
                    {
                        kq = BenhNhanBUS.InsertBenhNhan(benhnhan);
                        break;
                    }
                case 1:
                    {
                        kq = BenhNhanBUS.UpdateBenhNhan(benhnhan);
                        break;
                    }
            }
            if (kq)
            {
                LuuBenhTienSu(benhnhan.Ma);
                //MessageBox.Show("Thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.Yes;                
                this.Close();
            }
            else
            {
                //MessageBox.Show("Thông tin chưa chính xác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void LuuBenhTienSu(int mabn)
        {
            if (checkBox1.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 1, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 1);
            if (checkBox2.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 2, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 2);
            if (checkBox3.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 3, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 3);
            if (checkBox4.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 4, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 4);
            if (checkBox5.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 5, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 5);
            if (checkBox6.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 6, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 6);
            if (checkBox7.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 7, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 7);
            if (checkBox8.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 8, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 8);
            if (checkBox9.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 9, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 9);
            if (checkBox10.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 10, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 10);
            if (checkBox11.Checked)
                BenhNhanBUS.InsertBenhNhan_BenhTienSu(mabn, 11, txtBenhKhac.Text);
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 11);
        }

        private void txtMaHoSo_Leave(object sender, EventArgs e)
        {
            if (!txtMaHoSo.Text.Equals(""))
            {
                benhnhan = BenhNhanBUS.SelectBenhNhanByMa(int.Parse(txtMaHoSo.Text));
                LoadBenhNhan();
            }
        }
    }
}
